package ch.usi.inf.pf2.gui.chart;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;

import ch.usi.inf.pf2.gui.JSpreadSheetGUI;

public class PieChart extends JComponent
{
    JSpreadSheetGUI spreadTable;
    int[][] selectedCells;
    int[][] sizeOfSelected;
    int number;
    final int MAX_HEIGTH = 300;
    final int WIN_HEIGTH;

    /**
     * Create the bar chart.
     * 
     * @param spreadTable
     *            The table to which the built bar-chart is related to
     * @param selectedRows
     *            The rows of the selected cells
     * @param selectedColumns
     *            The columns of the selected cells
     * @param winHeigth
     *            The size of the chart window
     */
    public PieChart(JSpreadSheetGUI spreadTable, int[] selectedRows,
		  int[] selectedColumns, int winHeigth)
    {
	   this.spreadTable = spreadTable;
	   selectedCells = new int[selectedRows.length][selectedColumns.length];
	   sizeOfSelected = new int[selectedRows.length][selectedColumns.length];
	   number = selectedRows.length * selectedColumns.length;
	   for (int i = 0; i < selectedRows.length; i++)
		  for (int j = 0; j < selectedColumns.length; j++)
			 selectedCells[i][j] = (int) Double.parseDouble(spreadTable
				    .getSheet().getCell(selectedColumns[j] + 1,
						  selectedRows[i] + 1).getContent().toString());

	   WIN_HEIGTH = winHeigth - 20;
    }

    /**
     * @return The sum of the array's values
     */
    public int sumArray()
    {
	   int sum = 0;
	   for (int i = 0; i < selectedCells.length; i++)
		  for (int j = 0; j < selectedCells[i].length; j++)
			 sum += selectedCells[i][j];
	   return sum;
    }

    /**
     * Size of the elements related to the pie
     */
    private void sizeSelected()
    {
	   int tot = sumArray();
	   for (int i = 0; i < selectedCells.length; i++)
		  for (int j = 0; j < selectedCells[i].length; j++)
			 sizeOfSelected[i][j] = 360 / tot * selectedCells[i][j];
    }

    /**
     * @return a suitable width of the window fitting with the number of bars in
     *         the chart
     */
    public int width()
    {
	   return 400;
    }

    /**
     * Draw the chart.
     */
    public void paint(Graphics g)
    {
	   int p = 0;
	   int c = 0;
	   sizeSelected();
	   for (int i = 0; i < selectedCells.length; i++)
		  for (int j = 0; j < selectedCells[i].length; j++)
		  {
			 g.setColor(Color.black);
			 g.drawArc(width() / 4, MAX_HEIGTH / 2, width() / 2,
				    MAX_HEIGTH / 2, p, sizeOfSelected[i][j]);
			 g.setColor(ChartColor.selectColor(c + 1));
			 g.fillArc(width() / 4, MAX_HEIGTH / 2, width() / 2,
				    MAX_HEIGTH / 2, p, sizeOfSelected[i][j]);

			 if(i==selectedCells.length-1 && j==selectedCells[0].length-1)
			 {
				g.setColor(Color.black);
				 g.drawArc(width() / 4, MAX_HEIGTH / 2, width() / 2,
					    MAX_HEIGTH / 2, p, 360-p);
				 g.setColor(ChartColor.selectColor(c + 1));
				 g.fillArc(width() / 4, MAX_HEIGTH / 2, width() / 2,
					    MAX_HEIGTH / 2, p, 360-p);
			 }
			 
			 // g.drawString(selectedCells[i][j]+"", p*20, WIN_HEIGTH-h-5);
			 p += sizeOfSelected[i][j];
			 c++;
		  }
    }
}
